package com.huaishushu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaishushu.common.BaseContext;
import com.huaishushu.entity.ShoppingCart;
import com.huaishushu.mapper.ShoppingCartMapper;
import com.huaishushu.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author ChiBaoBaoHuaiShuShu
 * @since 2023-05-28
 */
@Slf4j
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 存储上一个菜品的口味,
     * 用来辅助菜品添加购物车,
     * 实现连续添加同一口味的菜品
     */
    private String dishFlavor;


    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //获取当前用户的id，并为其赋值
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //设置创建时间为当前时间
        shoppingCart.setCreateTime(LocalDateTime.now());
        //当时选择规格按钮添加菜品的时候，则必然会有口味，则这时更新口味
        if (shoppingCart.getDishFlavor() != null) {
            dishFlavor = shoppingCart.getDishFlavor();
        }
        //如果当前加入购物车的是个菜品，并且没有口味，说明目前调用的是连续添加同一口味的菜品
        if (shoppingCart.getDishId() != null && shoppingCart.getDishFlavor() == null) {
            //将选择规格时选择的口味赋值给它
            shoppingCart.setDishFlavor(dishFlavor);
        }

        //添加之前先查询当前口味的菜品或套餐是否存在于购物车
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        lqw.eq(ShoppingCart::getUserId, userId);
        lqw.eq(StringUtils.isNoneEmpty(shoppingCart.getDishFlavor()), ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());

        ShoppingCart cart = shoppingCartMapper.selectOne(lqw);

        //判断是否存在购物车
        if (cart != null) {
            //存在购物车则数量+1
            Integer number = cart.getNumber();
            cart.setNumber(number + 1);
            shoppingCartMapper.updateById(cart);
        } else {
            //不存在购物车，则将商品的数量设置为1
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);

            cart = shoppingCart;
        }
        return cart;
    }

    /**
     * 查询当前用户购物车内的商品信息
     * @return
     */
    @Override
    public List<ShoppingCart> listShoppingCart() {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        //按添加时间来个降序
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(lqw);

        return shoppingCartList;
    }
}
